package com.iut.projectandroid;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.iut.controls.ListParcelAdapter;
import com.iut.controls.ViewHolder;
import com.iut.model.BusinessLayer;
import com.iut.model.Parcelle;
import com.iut.utils.MenuHelper;

public class MainActivity extends Activity 
{
	public static final int ADD_PARCEL = 1;
	
	ListParcelAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		List<Parcelle> liste = BusinessLayer.parcelles(getApplicationContext(),Parcelle.class);
		
		ListView listParcel = (ListView) findViewById(R.id.listparcel);
		
		adapter = new ListParcelAdapter(getApplicationContext(),
												R.layout.list_item, liste);
		
		listParcel.setAdapter(adapter);
		listParcel.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) 
			{
				ViewHolder holder = (ViewHolder) arg1.getTag();
				final int parcelId = holder.getParcelId();
				
				AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
						MainActivity.this);
		 		 
					// set dialog message
					alertDialogBuilder
						.setMessage("Etes-vous sur de vouloir supprimer cette parcelle ? ")
						.setCancelable(true)
						.setPositiveButton("Oui",new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,int id) {
								BusinessLayer.deleteParcelle(getApplicationContext(), parcelId, Parcelle.class);
								
								adapter.notifyDataSetChanged();
							}
						  })
						.setNegativeButton("Non",new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,int id) {
								dialog.cancel();
							}
						});
		 
						// create alert dialog
						AlertDialog alertDialog = alertDialogBuilder.create();
		 
						// show it
						alertDialog.show();
						
						return true;
					}
		});
		
		listParcel.setOnItemClickListener(new OnItemClickListener() 
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) 
			{
				ViewHolder holder = (ViewHolder) arg1.getTag();
				int parcelId = holder.getParcelId();
				
				Intent intent = new Intent(getApplicationContext(), ParcelDetailActivity.class);
				intent.putExtra(ParcelDetailActivity.PARCEL_ID, parcelId);
				startActivity(intent);
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		if(requestCode == ADD_PARCEL)
		{
			if(resultCode == RESULT_OK)
				adapter.notifyDataSetChanged();
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		if(MenuHelper.launchActivity(this ,getApplicationContext(), item.getItemId()))
		{
			return true;
		}
		else if (item.getItemId() == R.id.add_menu)
		{
			openAddActivity();
			return true;
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}		
	}

	private void openAddActivity()
	{
		Intent intent = new Intent(getApplicationContext(), ParcelAddActivity.class);
		startActivityForResult(intent, ADD_PARCEL);
	}
}
